﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using RaveEngine.Engine.Query;

namespace RaveEngine.Engine
{
    public abstract class BaseRaveQuery 
    {
        public abstract object Process();
        public BaseRaveConfig config { get; set; }

        public BaseRaveQuery() { }

        public BaseRaveQuery(BaseRaveConfig c)
        {
            config = c;
        }

        public string Execute()
        {
            object item;
            string sReturn = string.Empty;

            if (config == null)
            {
                throw new NullReferenceException("Rave config was not set");
            }

            item = Process();
            
            switch (config.responseType)
            {
                case "xml":
                    XmlSerializer xml = new XmlSerializer(item.GetType());
                    StringBuilder sb = new StringBuilder();
                    StringWriter sw = new StringWriter(sb);
                    xml.Serialize(sw, item);
                    sReturn = sw.ToString();
                    break;
                default:
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    sReturn = js.Serialize(item);
                    break;
            }

            return sReturn;
        }
    }
}